subroutine set_group_type(gtype,ng,str)
  use param
  use derived,only:GroupType
  use global_param,only:pdefect
  use global_param,only:n_point_type
  implicit none

  type(GroupType),pointer,dimension(:) :: gtype
  integer,intent(in) :: ng
  character :: str*(*)

  integer :: i,n,l
  integer :: delimiter,asci_code,len_str,pos1,pos2
  integer :: typeid

  character(len=len_wordbuf),dimension(max_n_word) :: eatword

  delimiter=ichar('/')

  str=adjustl(str)
  len_str=len_trim(str)

  ! Consider the string as a number until '/' appears.
  do i=1,len_str
     asci_code=ichar(str(i:i))
     if (asci_code.eq.delimiter) pos1=i
     if (asci_code.eq.delimiter) exit
  end do

  ! Save as an index
  read(str(1:pos1-1),*) gtype(ng)%index
  pos1=pos1+1

  ! Determine the type of group and the groupwise condition
  do i=pos1,len_str
     asci_code=ichar(str(i:i))
     if (asci_code.eq.delimiter) pos2=i
     if (asci_code.eq.delimiter) exit
  end do

  ! Save the number to identify the type of the group
  read(str(pos1:pos2-1),*) typeid
  pos1=pos2+1

  ! Classify the sort of group in three
  ! case 1:configurational condition
  ! case 2:total size
  ! case 3:count whole number only for one kind defect
  if (typeid.ge.1.and.typeid.le.n_point_type) then
     gtype(ng)%group_type=1
     gtype(ng)%n_point=typeid
  else if (typeid.eq.(n_point_type+1)) then
     gtype(ng)%group_type=2
     gtype(ng)%n_point=1
  else if (typeid.eq.0) then
     gtype(ng)%group_type=3
     gtype(ng)%n_point=1
  end if

  typeid=gtype(ng)%n_point

  allocate(gtype(ng)%point_type(typeid))
  allocate(gtype(ng)%min_size(typeid))
  allocate(gtype(ng)%max_size(typeid))

  ! Initialize buffer
  do i=1,max_n_word
     eatword(i)=""
  end do

  read(str(pos1:len_str),*,end=100) (eatword(i),i=1,max_n_word)
100 continue

  if (gtype(ng)%group_type.eq.1) then
     do i=1,typeid
        l=len_trim(eatword(2*i-1))
        if (l.eq.0) then
           call terminal_msg1(str(1:len_str))
           call abnormal_stop("set_group_type",&
                "Missing the type of defect.")
          end if
        l=len_trim(eatword(2*i))
        if (l.eq.0) then
           call terminal_msg1(str(1:len_str))
           call abnormal_stop("set_group_type",&
                "Missing the group configuration.")
          end if
        call str2id(eatword(2*i-1),&
             gtype(ng)%point_type(i))
        call str2range(eatword(2*i),&
             gtype(ng)%min_size(i),&
             gtype(ng)%max_size(i))
     end do
  else if (gtype(ng)%group_type.eq.2) then
     gtype(ng)%point_type(1)=0
     call str2range(str(pos1:len_str),&
          gtype(ng)%min_size(1),&
          gtype(ng)%max_size(1))
  else
     call str2id(str(pos1:len_str),&
          gtype(ng)%point_type(1))
     gtype(ng)%min_size(1)=0
     gtype(ng)%max_size(1)=0
     ! only for type 3
     gtype(ng)%single_type=gtype(ng)%point_type(1)
  end if

  return

end subroutine set_group_type
